<template>
  <div>
    <div class="box">
      <van-nav-bar left-arrow @click-left="$router.go(-1)" />
      <div class="search">
        <div class="search-box">
          <div class="city">
            <span class="site" @click="$router.push('/citylist')" ref="dz"
              >上海</span>
            <van-icon name="arrow-down" />
          </div>
          <van-search
            v-model="value"
            placeholder="请输入搜索关键词"
            background="#fff"
          />
        </div>
        <div class="icon" @click="$router.push('/baidumap')">
          <van-icon name="location-o" color="#fff" size="18px" />
        </div>
      </div>
    </div>
    <div class="colbox" ref="col">
      <van-sticky>
        <div class="col">
          <van-row type="flex">
            <van-col span="6" style="height: 40px" @click="showfn"
              >区域<van-icon name="arrow-down" /></van-col
            >
            <van-col span="6" style="height: 40px" @click="show1fn"
              >方式<van-icon name="arrow-down" /></van-col
            >
            <van-col span="6" style="height: 40px" @click="show2fn"
              >租金<van-icon name="arrow-down" /></van-col
            >
            <van-col span="6" style="height: 40px" @click="show3fn"
              >筛选<van-icon name="arrow-down" /></van-col
            >
          </van-row>
        </div>
        <van-divider />
      </van-sticky>
    </div>
    <van-popup
      v-model="show"
      position="50px"
      :style="{ height: '40%', width: '100%', backgroundColor: 'pink' }"
      :get-container="getContainer"
      ><van-picker show-toolbar title="" :columns="fristList" @cancel="onCancel" @confirm="onConfirm"
    /></van-popup>

    <van-popup
      v-model="show1"
      position="50px"
      :style="{ height: '40%', width: '100%', backgroundColor: 'pink'}"
      :get-container="getContainer"
    >
      <van-picker show-toolbar title="" :columns="list"  @cancel="onCancel1" @confirm="onConfirm1"
    /></van-popup>

    <van-popup
      v-model="show2"
      position="50px"
      :style="{ height: '40%', width: '100%'}"
      :get-container="getContainer"
    >
      <van-picker show-toolbar title="" :columns="priceList" @cancel="onCancel2" @confirm="onConfirm2"/>
    </van-popup>

    <van-popup
      position="right"
      v-model="show3"
      :style="{ width: '75%', height: '100%' }"
    >
      <van-row gutter="20">
        <p>户型</p>
        <div class="spbox">
          <span
            v-for="(item, index) in roomTypeList"
            :key="index"
            @click="screenClick(item.value, $event)"
            ref="clear"
            >{{ item.label }}
          </span>
        </div>
      </van-row>

      <van-row gutter="20">
        <p>朝向</p>
        <div class="spbox">
          <span
            v-for="(item, index) in orientedList"
            :key="index"
            @click="screenClick(item.value,  $event)"
            ref="clear"
            >{{ item.label }}</span
          >
        </div>
      </van-row>

      <van-row gutter="20">
        <p>楼层</p>
        <div class="spbox">
          <span
            v-for="(item, index) in floorList"
            :key="index"
            @click="screenClick(item.value, $event)"
            ref="clear"
            >{{ item.label }}</span
          >
        </div>
      </van-row>

      <van-row gutter="20">
        <p>房屋特点</p>
        <div class="spbox">
          <span
            v-for="(item, index) in characteristicList"
            :key="index"
            @click="screenClick(item.value,  $event)"
            ref="clear"
            >{{ item.label }}</span
          >
        </div>
      </van-row>
      <div class="btnbox">
        <button class="btn3" @click="btn3Fn">清除</button>
        <button class="btn4" @click="btn4Fn">确认</button>
      </div>
    </van-popup>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了">
      <HouseList
        v-for="item in dataList"
        :key="item.id"
        :item="item"
      ></HouseList>
    </van-list>
  </div>
</template>

<script>
import { getStr } from '@/utils/storage'
import { getArea, getHouse } from '@/api/other'
import HouseList from '@/components/HouseItem.vue'
export default {
  components: {
    HouseList
  },
  data () {
    return {
      finished: true,
      loading: false,
      value: '',
      active: '',
      show: false,
      show1: false,
      show2: false,
      show3: false,
      informationArr: [],

      /* 区域 */
      fristList: [],
      /* 方式 */
      list: [],
      modeArr: [],
      modeValue: '',

      /* 价格 */
      priceList: [],
      priceArr: [],
      priceValue: '',

      /* 户型 */
      roomTypeList: [],

      /* 朝向 */
      orientedList: [],
      /* 楼层 */
      floorList: [],
      /* 房屋特点 characteristic */
      characteristicList: [],
      /* 页面数据 */
      dataList: []
    }
  },
  async created () {
    this.$nextTick(() => {
      this.$refs.dz.innerText = JSON.parse(getStr()).label
      this.informationArr.push(JSON.parse(getStr()).value)
    })
    const { body } = await getArea(JSON.parse(getStr()).value)
    /* 区域和地铁的一级列表 */
    this.fristList.push(body.area, body.subway)
    this.fristList[0] = this.areaList(this.fristList[0])
    this.fristList[1] = this.areaList(this.fristList[1])
    // console.log(this.fristList)
    console.log(body)
    /* 方式 */
    this.list = body.rentType.map((item) => item.label)
    this.modeArr = body.rentType

    /* 租金 */
    this.priceList = body.price.map((item) => item.label)
    this.priceArr = body.price

    /* 户型 */
    this.roomTypeList = body.roomType

    /* 朝向 */
    this.orientedList = body.oriented

    /* 楼层 */
    this.floorList = body.floor

    /* 房屋特点 characteristic */
    this.characteristicList = body.characteristic

    /* 页面渲染 */
    this.getData(this.informationArr.join(','))
  },
  methods: {
    getContainer () {
      return document.querySelector('.colbox')
    },
    areaList (arr) {
      const arrlist = { text: arr.label, children: arr.children }
      const array = []
      arrlist.children.forEach((element) => {
        const threeArr = []
        element.children
          ? element.children.forEach((item) =>
            threeArr.push({ text: item.label, children: item.children })
          )
          : threeArr.push({ text: '', value: null })
        element.children = threeArr
        array.push({ text: element.label, children: element.children })
      })
      arrlist.children = array
      // console.log(arrlist)
      return arrlist
    },
    screenClick (value, e) {
      // console.log(value, index)
      if (this.informationArr.indexOf(value) === -1) {
        this.informationArr.push(value)
        e.target.style.color = 'hotpink'
        e.target.style.backgroundColor = 'pink'
      } else {
        this.informationArr.splice(this.informationArr.indexOf(value), 1)
        e.target.style.color = ''
        e.target.style.backgroundColor = ''
      }
      // console.log(this.informationArr)
    },
    btn3Fn () {
      this.informationArr.splice(1, this.informationArr.length - 1)
      // console.log(this.informationArr)
      // console.log(this.$refs.clear)
      this.informationArr.push(this.priceValue)
      this.informationArr.push(this.modeValue)
      this.$refs.clear.forEach((item) => {
        item.style.color = ''
        item.style.backgroundColor = ''
      })
    },
    btn4Fn () {
      this.getData(this.informationArr.join(','))
      this.show3 = false
    },
    /* 请求数据 */
    async getData (more) {
      const res = await getHouse(more)
      // console.log(res)
      this.dataList = res.body.list
    },
    onCancel () {
      this.show = false
    },
    onConfirm (value1) {
      this.getData(this.informationArr.join(','))
      this.show = false
    },
    onCancel1 () {
      this.show1 = false
    },
    onConfirm1 (value1) {
      // console.log(value1)
      // console.log(this.priceArr)
      const values = this.modeArr.find(item => item.label === value1).value
      // console.log(values)
      this.modeValue = values
      this.informationArr.push(this.modeValue)
      this.getData(this.informationArr.join(','))
      this.show1 = false
    },
    onCancel2 () {
      this.show2 = false
    },
    onConfirm2 (value1) {
      // console.log(value1)
      // console.log(this.priceArr)
      const values = this.priceArr.find(item => item.label === value1).value
      // console.log(values)
      this.priceValue = values
      this.informationArr.push(this.priceValue)
      this.getData(this.informationArr.join(','))
      this.show2 = false
    },

    showfn () {
      this.show = true
      this.show1 = false
      this.show2 = false
      this.show3 = false
    },
    show1fn () {
      this.show1 = true
      this.show = false
      this.show2 = false
      this.show3 = false
    },
    show2fn () {
      this.show2 = true
      this.show = false
      this.show1 = false
      this.show3 = false
    },
    show3fn () {
      this.show3 = true
      this.show = false
      this.show1 = false
      this.show2 = false
    }
  }
}
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.box {
  position: relative;
  height: 60px;
}
.search {
  position: absolute;
  top: 14px;
  // background-color: pink;
  z-index: 2;
  height: 30px;
  display: flex;
  align-items: center;
  margin-left: 60px;
  .search-box {
    /*  min-width: 250px; */
    display: flex;
    text-align: center;
    margin-right: 10px;

    background-color: #fff;
    border-radius: 5px;
    overflow: hidden;
    padding: 10px 0px;
    .van-cell.van-cell--borderless.van-field {
      padding: 0;
      background-color: #fff;
      height: 20px;
    }
    .city {
      background-color: #fff;
      height: 20px;
      line-height: 20px;
      padding: 0px 10px;
      border-right: 1px solid #e5e5e5;
      margin-right: 10px;
    }
  }
  .icon {
    width: 30px;
    height: 30px;
    border: 2.5px solid #fff;
    border-radius: 50%;
    text-align: center;
    line-height: 30px;
  }
}
.col {
  height: 40px;
  .van-col {
    text-align: center;
    line-height: 40px;
    font-size: 18px;
    z-index: 100;
    background-color: #fff;
  }
}
.spbox {
  margin: 10px 10px 20px 40px;
  padding-bottom: 20px;
  border-bottom: 1px solid#ccc;
  span {
    border: 1px solid#ccc;
    display: inline-block;
    border-radius: 4px;
    width: 18.33333vw;
    height: 30px;
    line-height: 30px;
    margin-top: 10px;
    margin-right: 10px;
    text-align: center;
    color: #999;
  }
}
.van-row {
  p {
    margin-left: 10px;
  }
}

.van-popup--top {
  top: 7%;
}
.btn3 {
  width: 40%;
  position: absolute;
  color: #21b97a;
  background-color: #fff;
  border: none;
  // bottom: 7px;
  left: 0;
  z-index: 999;
  height: 50px;
  // bottom: 0;
}
.btn4 {
  position: absolute;
  width: 60%;
  color: #fff;
  background-color: #21b97a;
  border: none;
  right: 0;
  // bottom: 7px;
  z-index: 999;
  height: 50px;
  // bottom: 0;
}
</style>
